package cn.toleo.search.listener;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.MessageCreator;

import cn.toleo.common.pojo.SearchItem;
import cn.toleo.search.mapper.SearchItemMapper;

/**
 * 监听商品事件添加,同步索引库
 * @author Administrator
 *
 */
public class ItemAddMessageListener implements MessageListener{
	@Autowired
	private SearchItemMapper searchItemMapper;
	@Autowired
	private SolrServer solrServer;

	@Override
	public void onMessage(Message message) {
	try{	
		//主干基础
		TextMessage textMessage=(TextMessage)message;
		String text = textMessage.getText();
		Long itemId=Long.parseLong(text);
		Thread.sleep(1000);
		SearchItem searchItem=searchItemMapper.getItemById(itemId);
		//创建文档对象
		SolrInputDocument document = new SolrInputDocument();
		//向文档中添加域
		document.addField("id", searchItem.getId());
		document.addField("item_title", searchItem.getTitle());
		document.addField("item_sell_point", searchItem.getSell_point());
		document.addField("item_price", searchItem.getPrice());
		document.addField("item_image", searchItem.getImage());
		document.addField("item_category_name", searchItem.getCategory_name());
		document.addField("item_desc", searchItem.getItem_desc());
		//主干
		solrServer.add(document);
		
		//结果
		solrServer.commit();
	}catch(Exception e){
		e.printStackTrace();
	}
		
	}
	
	

}
